package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.model.domain.Admin;
import com.example.mybatisplus.mapper.AdminMapper;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhang
 * @since 2021-07-05
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    /**
     * 管理员注册server
     * @param admin
     * @return
     */
    @Override
    public String register(Admin admin) {
        // 查找用户  如果查找成功就返回已经注册  未查找到就插入数据库
        QueryWrapper<Admin> wr = new QueryWrapper<>();
        wr.eq("admin_name",admin.getAdminName());
        List<Admin> admins = adminMapper.selectList(wr);
        if (admins.size()>0){
            return "409";
        }else {
            Admin admininst = new Admin().setId(admin.getId())
                    .setAdminName(admin.getAdminName())
                    .setPassword(admin.getPassword())
                    .setRegisterTime(admin.getRegisterTime())
                    .setMember(admin.getMember());
            adminService.save(admininst);
            return "200";
        }
    }

    @Override
    public Admin login(String username, String password) {
        //从controller到service
        QueryWrapper<Admin> wapper=new QueryWrapper<>();
        wapper.eq("admin_name",username).eq("password",password);
        List<Admin> admins = adminMapper.selectList(wapper);
        return  admins.size()>0?admins.get(0):null;
    }

    @Override
    public Integer adminAddUser(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",user.getUserName()).eq("phone",user.getPhone());
        List<User> usyur = userMapper.selectList(wrapper);
        if(usyur.size()>0){
         return 600;
        }else {
            userService.save(user);
            return 200;
        }
    }
   //根据user_name查找用户信息
    @Override
    public List<User> select(String userName) {
        QueryWrapper<User> wr = new QueryWrapper<>();
        wr.eq("user_name",userName);
        List<User> users =userMapper.selectList(wr);
        return users;
    }

    // 编辑用户dao
    @Override
    public Integer adminEditUser(User user) {
        // 先按照id查找用户
        //修改条件s
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.eq("id", user.getId());
        // 返回更新操作所涉及的条数
        return userMapper.update(user, userUpdateWrapper);
    }
}
